package com.fs.web.base.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.common.baseMapper.IntegralMapper;
import com.fs.common.pojo.Integral;
import com.fs.common.pojo.User;
import com.fs.web.base.enums.CodeEnum;
import com.fs.web.base.utils.ApplicationUtils;
import com.fs.web.base.utils.PageHeper;
import com.fs.web.base.vo.DataGrid;
import com.fs.web.base.vo.JsonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/console/integral/rewrite")
public class IntegralController {
    @Autowired
    private IntegralMapper integralMapper;

    @ResponseBody
    @RequestMapping("/list")
    public JsonResponse list(HttpServletRequest request){
        try {
            User user = ApplicationUtils.getUser().getUser();
            Page page = PageHeper.pageRequest(request);
            Map<String, Object> map = ApplicationUtils.getRequestDatas();
            String userId = null;
            if (!user.isSysAdm()) {  // 管理员返回全部信息,个人用户只返回个人信息
                userId = user.getId();
            }
            Page<Integral> all = integralMapper.findAll(page, map, userId);
            return JsonResponse.success(DataGrid.gridBuilder(all));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return JsonResponse.error(CodeEnum.EX999);
        }
    }
}
